package com.shrct.businessprocess.project.impl;

import com.google.common.base.Objects;
import com.shrct.businessprocess.project.RCProject;
import com.shrct.businessprocess.project.RCProjectProductSale;
import com.shrct.businessprocess.project.RCProjectServiceDeliver;
import com.shrct.businessprocess.project.RCProjectServiceDuration;
import com.shrct.businessprocess.project.RCProjectTypeExtension;
import com.shrct.businessprocess.project.RCProjectTypeExtensionDAO;
import com.shrct.businessprocess.project.impl.RCProjectProductSaleImpl;
import com.shrct.businessprocess.project.impl.RCProjectServiceDeliverImpl;
import com.shrct.businessprocess.project.impl.RCProjectServiceDurationImpl;
import edu.fudan.mylang.pf.BaseDAOService;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.springframework.stereotype.Service;

@Service("com.shrct.businessprocess.project.RCProjectTypeExtensionDAO")
public class RCProjectTypeExtensionDAOImpl extends BaseDAOService<RCProjectTypeExtension> implements RCProjectTypeExtensionDAO {
  public RCProjectTypeExtension asExtension(final RCProject project, final Function1<? super Void,? extends RCProjectTypeExtension> creator) {
    final RCProjectTypeExtension old = project.getTypeExtension();
    project.setTypeExtension(null);
    boolean _notEquals = (!Objects.equal(old, null));
    if (_notEquals) {getObjectFactory().delete(old);
      
    }
    RCProjectTypeExtension _apply = creator.apply(null);
    final Procedure1<RCProjectTypeExtension> _function = new Procedure1<RCProjectTypeExtension>() {
        public void apply(final RCProjectTypeExtension it) {
          it.setProject(project);
          it.initType();
        }
      };
    RCProjectTypeExtension _doubleArrow = ObjectExtensions.<RCProjectTypeExtension>operator_doubleArrow(_apply, _function);
    project.setTypeExtension(_doubleArrow);
    return project.getTypeExtension();
  }
  
  public RCProjectProductSale asProductSale(final RCProject project) {
    RCProjectProductSale _xblockexpression = null;
    {
      RCProjectTypeExtension _typeExtension = project.getTypeExtension();
      boolean _not = (!(_typeExtension instanceof RCProjectProductSale));
      if (_not) {
        final Function1<Void,RCProjectProductSale> _function = new Function1<Void,RCProjectProductSale>() {
            public RCProjectProductSale apply(final Void it) {RCProjectProductSale rCProjectProductSale=RCProjectProductSaleImpl.create(getObjectFactory());
              return rCProjectProductSale;
            }
          };
        this.asExtension(project, _function);
      }
      RCProjectTypeExtension _typeExtension_1 = project.getTypeExtension();
      _xblockexpression = (((RCProjectProductSale) _typeExtension_1));
    }
    return _xblockexpression;
  }
  
  public RCProjectServiceDeliver asDeliver(final RCProject project) {
    RCProjectServiceDeliver _xblockexpression = null;
    {
      RCProjectTypeExtension _typeExtension = project.getTypeExtension();
      boolean _not = (!(_typeExtension instanceof RCProjectServiceDeliver));
      if (_not) {
        final Function1<Void,RCProjectServiceDeliver> _function = new Function1<Void,RCProjectServiceDeliver>() {
            public RCProjectServiceDeliver apply(final Void it) {RCProjectServiceDeliver rCProjectServiceDeliver=RCProjectServiceDeliverImpl.create(getObjectFactory());
              final Procedure1<RCProjectServiceDeliver> _function = new Procedure1<RCProjectServiceDeliver>() {
                  public void apply(final RCProjectServiceDeliver it) {
                    Boolean _allTaskDelivered = project.getAllTaskDelivered();
                    it.setDelivered(_allTaskDelivered);
                  }
                };
              RCProjectServiceDeliver _doubleArrow = ObjectExtensions.<RCProjectServiceDeliver>operator_doubleArrow(rCProjectServiceDeliver, _function);
              return _doubleArrow;
            }
          };
        this.asExtension(project, _function);
      }
      RCProjectTypeExtension _typeExtension_1 = project.getTypeExtension();
      _xblockexpression = (((RCProjectServiceDeliver) _typeExtension_1));
    }
    return _xblockexpression;
  }
  
  public RCProjectServiceDuration asServiceDuration(final RCProject project, final Procedure1<? super RCProjectServiceDuration> init) {
    RCProjectServiceDuration _xblockexpression = null;
    {
      RCProjectTypeExtension _typeExtension = project.getTypeExtension();
      boolean _not = (!(_typeExtension instanceof RCProjectServiceDuration));
      if (_not) {
        final Function1<Void,RCProjectServiceDuration> _function = new Function1<Void,RCProjectServiceDuration>() {
            public RCProjectServiceDuration apply(final Void it) {RCProjectServiceDuration rCProjectServiceDuration=RCProjectServiceDurationImpl.create(getObjectFactory());
              return rCProjectServiceDuration;
            }
          };
        this.asExtension(project, _function);
      }
      RCProjectTypeExtension _typeExtension_1 = project.getTypeExtension();
      final RCProjectServiceDuration result = ((RCProjectServiceDuration) _typeExtension_1);
      init.apply(result);
      _xblockexpression = (result);
    }
    return _xblockexpression;
  }
}
